﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace 刘颜红销售项目管理.Business
{
    public class ListContainer
    {

        public static object CreateList( ListContainer container , Type listType , Type itemType )
        {
            System.Collections.IList newList = ( System.Collections.IList ) System.Activator.CreateInstance(listType);
            if ( container != null && container._innerList != null)
            {
                foreach (object item in container._innerList)
                {
                    if (itemType != null)
                    {
                        Type t = item.GetType();
                        if (t.Equals(itemType) || t.IsSubclassOf(itemType))
                        {
                            newList.Add(item);
                        }
                    }
                    else
                    {
                        newList.Add(item);
                    }
                }//foreach
            }
            return newList;
        }

        public ListContainer()
        {
            _innerList = new ArrayList();
        }

        public ListContainer(IList list)
        {
            _innerList = list;
        }

        private IList _innerList = null;

        public IList InnerList
        {
            get { return _innerList; }
            set { _innerList = value; }
        }

        public int Count
        {
            get
            {
                if (_innerList == null)
                    return 0;
                else
                    return _innerList.Count;
            }
        }

        public int Add(object item)
        {
            return _innerList.Add(item);
        }

        public void Remove(object item)
        {
            _innerList.Remove(item);
        }

        public void RemoveAt(int index)
        {
            _innerList.RemoveAt(index);
        }

        public object this[int index]
        {
            get
            {
                return _innerList[index];
            }
            set
            {
                _innerList[index] = value;
            }
        }

        public void FillTo(IList list)
        {
            if (this._innerList != null)
            {
                foreach (object item in this._innerList)
                {
                    list.Add(item);
                }
            }
        }


        public override string ToString()
        {
            if (this._innerList == null)
            {
                return "";
            }
            else
            {
                StringBuilder str = new StringBuilder();
                foreach (object item in this._innerList)
                {
                    if (item != null)
                    {
                        if (str.Length > 0)
                        {
                            str.Append(",");
                        }
                        str.Append(Convert.ToString(item));
                    }
                }
                return str.ToString();
            }
        }
    }
}
